package bairui.international.weixin.portal.miniapp.controller.user;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lianjing.sh.spring.web.controller.base.action.Action;
import com.lianjing.sh.spring.web.controller.enhance.output.FomatOutput;

import bairui.international.model.domain.jpa.TB_Product;
import bairui.international.service.Service_ProxyAreaAddress;
import bairui.international.weixin.portal.miniapp.controller.product.product.C_Product_ShowImg_Get;
import lombok.AllArgsConstructor;
import lombok.Getter;

/**
 *
 * 代理
 *
 * @generated
 * 
 * @see STest_C_User_Proxy_Page 测试
 *
 */
@Controller
public class C_User_Proxy_Page
 extends SC_User_Proxy_Page 
{
	
	@Autowired private Service_ProxyAreaAddress service_ProxyAreaAddress;
	
	/**
	 * 列表
	 */
	@Action(Page.class)
	@GetMapping(Page.RequestMappingValue)
	public @FomatOutput @ResponseBody Object page(
		ModelMap map, HttpServletRequest request,
		@RequestParam Integer startPage,
		@RequestParam String skey
	) throws Exception {
		return service_ProxyAreaAddress
				.page(
					it->it.user.skey.eq(skey),
					PageRequest.of(startPage, 6)
				)
				.map(it->{
					return new Output(
//								it.getProduct().getId(),
								it.getTown().getId(),
								it.getAddress(),
								it.getFixAddress(),
								it.getPhone(),
								it.getUsername(),
								null
//								service_ProxyArea
//									.list(it2->it2.proxyAreaAddress.eq(it))
//									.stream()
//									.map(it2->{
//										return new OutputProduct(it2.getProduct());
//									})
//									.collect(Collectors.toList())
							);
				});
	}
	
	@AllArgsConstructor
	public static class Output {
//		private @Getter Long productId;
		private @Getter Long townId;
		private @Getter String address;
		private @Getter String fixAddress;
		private @Getter String phone;
		private @Getter String username;
		private @Getter List<OutputProduct> products;
	}
	
	public static class OutputProduct {
		public OutputProduct(TB_Product product){
			this.id = product.getId();
			this.imgUrl = new C_Product_ShowImg_Get.Get.Operate() {
				@Override public Object getId() {
					return product.getId();
				}
			}.getUrl();
			this.price = product.getPrice();
			this.name = product.getName();
		}
		private @Getter Long id;
		private @Getter String imgUrl;
		private @Getter Double price;
		private @Getter String name;
	}

}
